﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class Main_Utilities_Country : System.Web.UI.Page
{
    string sql = "";
    Datautility du = new Datautility();
    protected void Page_Load(object sender, EventArgs e)
    {
          if (Session["userid"] == null)
        {
            //ScriptManager.RegisterStartupScript(Page, this.GetType(), "redirect", "window.open('../../login.aspx','_parent');", true);
            Response.Redirect("~/SessionExpired.aspx");
        }
          if (Request.QueryString["action"] != null)
          {
              if (Request.QueryString["action"].ToString() == "addnew")
              {
                  PnlAddNew.Visible = true;
                  if (!IsPostBack)
                  {
                  }
              }
              else if (Request.QueryString["action"].ToString() == "view")
              {
                  if (!IsPostBack)
                  {
                      PnlView.Visible = true;
                      BindGridCountry();
                  }
              }
          }
    }

    protected void SaveCountry()
    {
        try
        {
            SqlParameter[] parmcountry = { 
                                             new SqlParameter("@countryname",txtcountry.Text.Trim())
                                         };
            du.getdatausingprocedure("usp_mas_savecountryname", parmcountry);
            lblMsg.Attributes.Remove("class");
            lblMsg.Attributes.Add("class", "true");
            lblMsg.Text = "Country Saved";
        }        
        catch(Exception ex)
        {
            lblMsg.Text = ex.Message;
            lblMsg.Attributes.Add("class", "false");
        }
    }

    protected void BindGridCountry()
    {
        sql = "select CountryId, CountryName from mas_country";
        DataTable dt = du.getdata(sql);
        GrdCountry.DataSource = dt;
        GrdCountry.DataBind();
    }

    protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        sql = "select Count(CountryName) from mas_country where CountryName ='" + txtcountry.Text +"'";
        DataTable dt = du.getdata(sql);
        if (dt.Rows[0][0].ToString() != "0" )
        {
            lblMsg.Attributes.Add("class", "false");
            lblMsg.Text = "Country Name Already Exists!";   
        }
        else 
        {
            SaveCountry();
            Response.Redirect("country.aspx?action=view");
            
        }      
    }
    protected void BtnCancel_Click(object sender, EventArgs e)
    {
       
    }
    protected void GrdCountry_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GrdCountry.EditIndex = e.NewEditIndex;
        BindGridCountry();
    }
    protected void GrdCountry_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {        
        TextBox txtcountry = (TextBox)GrdCountry.Rows[e.RowIndex].Cells[1].Controls[0];
        string Key = GrdCountry.DataKeys[e.RowIndex].Value.ToString();        
        if (txtcountry.Text.Trim() != string.Empty)
        {

            sql = "select CountryId from mas_country where Countryname = '" + txtcountry.Text + "'and CountryId!=" + Key + "";            
           
            DataTable dt = du.getdata(sql);
            if (dt.Rows.Count> 0 )
            {
                lblMsg.Attributes.Add("class", "false");
                lblMsg.Text = "Country already exists! ";
            }
            else
            {
                SqlParameter[] parm = { new SqlParameter("@countryname", txtcountry.Text.Trim()),
                                        new SqlParameter("@countryid", Key)
                                      };
                du.executeprocedure("usp_mas_UpdateCountry", parm);                
                GrdCountry.EditIndex = -1;
                BindGridCountry();              
                lblMsg.Attributes.Add("class", "true");
                lblMsg.Text = "Country Name updated successfully...";
            }
        }
        GrdCountry.EditIndex = -1;
        BindGridCountry();
    }
    protected void GrdCountry_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GrdCountry.EditIndex = -1;
        BindGridCountry();
    }
    protected void GrdCountry_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GrdCountry.PageIndex = e.NewPageIndex;
        BindGridCountry();
    }
}
